package gui;



import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.*;
import source.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author padsilva
 */
public class MenuRegisto extends javax.swing.JFrame {

    DatabaseHandler connect;
    File file;
    /**
     * Creates new form MenuRegisto
     */
    public MenuRegisto(DatabaseHandler conn) {
        initComponents();
        procuraFoto.setEnabled(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screenSize.width - this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2);
        connect=conn;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser6 = new javax.swing.JFileChooser();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        label1 = new javax.swing.JLabel();
        nomeUtilizador = new javax.swing.JTextField();
        label2 = new javax.swing.JLabel();
        label3 = new javax.swing.JLabel();
        label5 = new javax.swing.JLabel();
        primeiroUltimoNome = new javax.swing.JTextField();
        label6 = new javax.swing.JLabel();
        diaNascimento = new javax.swing.JTextField();
        barra = new javax.swing.JLabel();
        mesNascimento = new javax.swing.JTextField();
        anoNascimento = new javax.swing.JTextField();
        label7 = new javax.swing.JLabel();
        label8 = new javax.swing.JLabel();
        pais = new javax.swing.JTextField();
        localidade = new javax.swing.JTextField();
        label9 = new javax.swing.JLabel();
        label10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaInteresses = new javax.swing.JTextArea();
        confirmaAnexar = new javax.swing.JCheckBox();
        procuraFoto = new javax.swing.JButton();
        label11 = new javax.swing.JLabel();
        password = new javax.swing.JPasswordField();
        repetePassword = new javax.swing.JPasswordField();
        botaoRegistarParaMenuLogin = new javax.swing.JButton();
        botaoParaMenuEntrada = new javax.swing.JButton();
        label4 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        barra1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        genreM = new javax.swing.JRadioButton();
        genreF = new javax.swing.JRadioButton();
        perfilPublico = new javax.swing.JRadioButton();
        perfilPrivado = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        label1.setText("Insira o nome de utilizador:*");

        nomeUtilizador.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nomeUtilizadorMouseClicked(evt);
            }
        });

        label2.setText("Insira a password desejada:*");

        label3.setText("Repita a password desejada:*");

        label5.setText("Primeiro e último nome:*");

        primeiroUltimoNome.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                primeiroUltimoNomeMouseClicked(evt);
            }
        });

        label6.setText("Data de nascimento (aa/mm/dd):");

        diaNascimento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                diaNascimentoMouseClicked(evt);
            }
        });
        diaNascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                diaNascimentoActionPerformed(evt);
            }
        });

        barra.setText("/");

        mesNascimento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mesNascimentoMouseClicked(evt);
            }
        });
        mesNascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mesNascimentoActionPerformed(evt);
            }
        });

        anoNascimento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                anoNascimentoMouseClicked(evt);
            }
        });

        label7.setText("Sexo:*");

        label8.setText("País:*");

        pais.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                paisMouseClicked(evt);
            }
        });

        localidade.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                localidadeMouseClicked(evt);
            }
        });

        label9.setText("Cidade / Localidade:*");

        label10.setText("Insira os seus interesses:");

        listaInteresses.setColumns(20);
        listaInteresses.setRows(5);
        jScrollPane1.setViewportView(listaInteresses);

        confirmaAnexar.setText("Anexar Foto");
        confirmaAnexar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                confirmaAnexarMouseClicked(evt);
            }
        });
        confirmaAnexar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmaAnexarActionPerformed(evt);
            }
        });

        procuraFoto.setText("Procurar");
        procuraFoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                procuraFotoActionPerformed(evt);
            }
        });

        label11.setText("Perfil:*");

        password.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                passwordMouseClicked(evt);
            }
        });

        repetePassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                repetePasswordMouseClicked(evt);
            }
        });

        botaoRegistarParaMenuLogin.setText("REGISTAR");
        botaoRegistarParaMenuLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoRegistarParaMenuLoginActionPerformed(evt);
            }
        });

        botaoParaMenuEntrada.setText("Voltar");
        botaoParaMenuEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoParaMenuEntradaActionPerformed(evt);
            }
        });

        label4.setText("Insira o seu e-mail:*");

        email.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                emailMouseClicked(evt);
            }
        });

        barra1.setText("/");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        jLabel1.setText("Os campos assinalados com * são obrigatórios.");

        buttonGroup1.add(genreM);
        genreM.setText("Masculino");
        genreM.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        genreM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                genreMActionPerformed(evt);
            }
        });

        buttonGroup1.add(genreF);
        genreF.setText("Feminino");
        genreF.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        buttonGroup2.add(perfilPublico);
        perfilPublico.setText("Público");

        buttonGroup2.add(perfilPrivado);
        perfilPrivado.setText("Privado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(121, 121, 121)
                        .addComponent(botaoRegistarParaMenuLogin))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label6)
                        .addGap(18, 18, 18)
                        .addComponent(diaNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(barra)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(mesNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(barra1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(anoNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(label7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(genreM)
                                .addComponent(genreF))
                            .addGap(12, 12, 12)
                            .addComponent(label8)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(pais, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(label1)
                                .addComponent(label2)
                                .addComponent(label3)
                                .addComponent(label5)
                                .addComponent(label4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(nomeUtilizador)
                                .addComponent(primeiroUltimoNome)
                                .addComponent(password)
                                .addComponent(repetePassword)
                                .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(label9)
                            .addComponent(label10)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(perfilPrivado)
                                    .addComponent(perfilPublico))))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(localidade)
                                .addComponent(jScrollPane1))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(confirmaAnexar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botaoParaMenuEntrada)
                                    .addComponent(procuraFoto))))))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1)
                    .addComponent(nomeUtilizador, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label2)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label3)
                    .addComponent(repetePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label4)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label5)
                    .addComponent(primeiroUltimoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label6)
                    .addComponent(diaNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(barra)
                    .addComponent(mesNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(anoNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(barra1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(genreM)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(genreF)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(label7)
                                .addGap(27, 27, 27))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(label8)
                                    .addComponent(pais, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(localidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(label11)
                            .addComponent(perfilPublico))
                        .addGap(3, 3, 3)
                        .addComponent(perfilPrivado))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(procuraFoto)
                            .addComponent(confirmaAnexar))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botaoRegistarParaMenuLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botaoParaMenuEntrada))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void diaNascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_diaNascimentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_diaNascimentoActionPerformed

    private void botaoParaMenuEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoParaMenuEntradaActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        MenuEntrada menu = new MenuEntrada(connect);
        
    }//GEN-LAST:event_botaoParaMenuEntradaActionPerformed

    private void nomeUtilizadorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nomeUtilizadorMouseClicked
        // TODO add your handling code here:
        nomeUtilizador.setText("");
    }//GEN-LAST:event_nomeUtilizadorMouseClicked

    private void passwordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_passwordMouseClicked
        // TODO add your handling code here:
        password.setText("");
    }//GEN-LAST:event_passwordMouseClicked

    private void repetePasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_repetePasswordMouseClicked
        // TODO add your handling code here:
        repetePassword.setText("");
    }//GEN-LAST:event_repetePasswordMouseClicked

    private void emailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_emailMouseClicked
        // TODO add your handling code here:
        email.setText("");
    }//GEN-LAST:event_emailMouseClicked

    private void primeiroUltimoNomeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_primeiroUltimoNomeMouseClicked
        // TODO add your handling code here:
        primeiroUltimoNome.setText("");
    }//GEN-LAST:event_primeiroUltimoNomeMouseClicked

    private void diaNascimentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_diaNascimentoMouseClicked
        // TODO add your handling code here:
        diaNascimento.setText("");
    }//GEN-LAST:event_diaNascimentoMouseClicked

    private void mesNascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mesNascimentoActionPerformed
        // TODO add your handling code here:
        mesNascimento.setText("");
    }//GEN-LAST:event_mesNascimentoActionPerformed

    private void mesNascimentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mesNascimentoMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_mesNascimentoMouseClicked

    private void anoNascimentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_anoNascimentoMouseClicked
        // TODO add your handling code here:
        anoNascimento.setText("");
    }//GEN-LAST:event_anoNascimentoMouseClicked

    private void paisMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_paisMouseClicked
        // TODO add your handling code here:
        pais.setText("");
    }//GEN-LAST:event_paisMouseClicked

    private void localidadeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_localidadeMouseClicked
        // TODO add your handling code here:
        localidade.setText("");
    }//GEN-LAST:event_localidadeMouseClicked

    private void botaoRegistarParaMenuLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoRegistarParaMenuLoginActionPerformed
        // TODO add your handling code here:
        //campos obrigatórios!!!
        String passwordR="";
        String newfilename="";
        if(nomeUtilizador.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'nome de utilizador' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(!connect.checkUserToRegistry(nomeUtilizador.getText()).equals("null"))
        {
            JOptionPane.showMessageDialog(this, "'Nome de utilizador' já se encontra registado! Por favor escolha outro.", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(password.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'password' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(repetePassword.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'repita password' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(!password.getText().equals(repetePassword.getText()))
        {
            JOptionPane.showMessageDialog(this, "Passwords não coincidem!\nTente Novamente...", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        else {
            try {
                passwordR=password.getText();
                PasswordEncrypt.MD5(passwordR);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(MenuRegisto.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(MenuRegisto.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if(email.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'e-mail' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(primeiroUltimoNome.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'primeiro e último nome' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(!genreM.isSelected() && !genreF.isSelected())
        {
            JOptionPane.showMessageDialog(this, "O campo 'sexo' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }

        String sexo = "";
        if(genreM.isSelected())
        {
            sexo = "Masculino";
        }
        else if (genreF.isSelected())
        {
            sexo = "Feminino";
        }
        
        if(pais.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'País' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(localidade.getText().equals(""))
        {
            JOptionPane.showMessageDialog(this, "O campo 'Cidade/Localidade' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if(!perfilPublico.isSelected() && !perfilPrivado.isSelected())
        {
            JOptionPane.showMessageDialog(this, "O campo 'perfil' é de preenchimento obrigatório!", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }

        boolean perfil = false;
        if(perfilPublico.isSelected()){
            perfil = true;
        }
        else if (perfilPrivado.isSelected()){
            perfil = false;
        }
        
        String data = anoNascimento.getText() + "/" + mesNascimento.getText() + "/" + diaNascimento.getText();
        if(data.equals("//"))
        {
            data = "00/00/00";
        }
        
        BufferedImage img = null;
        if(confirmaAnexar.isSelected()){
            
             //generate timestamp string
               newfilename = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
               //generate control number (security reasons)
               Random r = new Random();
               int R = r.nextInt(99999-10000) + 10000;

               newfilename += Integer.toString(R);
               
               String user = System.getProperty("user.name");
            
               String url="C:/Users/"+user+"/Documents/NetBeansProjects/BD2012/images/"+newfilename+".jpg";
            try {
                img = ImageIO.read(this.file);
                File outputfile = new File(url);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException ex) {
                Logger.getLogger(MenuRegisto.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        if (connect.addUser(primeiroUltimoNome.getText(), nomeUtilizador.getText(), passwordR, data, sexo, email.getText(), pais.getText(), localidade.getText(), listaInteresses.getText(),newfilename ,perfil, false)) {
            this.setVisible(false);
            MenuLogin menu = new MenuLogin(connect);
            menu.setVisible(true);
        }
        else{
            JOptionPane.showMessageDialog(this, "Registo não efetuado.", "Erro", JOptionPane.ERROR_MESSAGE);
            return;
        }
            
        
    }//GEN-LAST:event_botaoRegistarParaMenuLoginActionPerformed

    private void procuraFotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_procuraFotoActionPerformed
        JFileChooser fileChooser= new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int retval = fileChooser.showDialog(this, null);       
        file = fileChooser.getSelectedFile();
    }//GEN-LAST:event_procuraFotoActionPerformed

    private void confirmaAnexarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmaAnexarActionPerformed
        // TODO add your handling code here:
        if(confirmaAnexar.isEnabled())procuraFoto.setEnabled(true);
        else procuraFoto.setEnabled(false);
    }//GEN-LAST:event_confirmaAnexarActionPerformed

    private void confirmaAnexarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_confirmaAnexarMouseClicked
        // TODO add your handling code here:
        if(confirmaAnexar.isEnabled())procuraFoto.setEnabled(true);
        else procuraFoto.setEnabled(false);
    }//GEN-LAST:event_confirmaAnexarMouseClicked

    private void genreMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_genreMActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_genreMActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField anoNascimento;
    private javax.swing.JLabel barra;
    private javax.swing.JLabel barra1;
    private javax.swing.JButton botaoParaMenuEntrada;
    private javax.swing.JButton botaoRegistarParaMenuLogin;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JCheckBox confirmaAnexar;
    private javax.swing.JTextField diaNascimento;
    private javax.swing.JTextField email;
    private javax.swing.JRadioButton genreF;
    private javax.swing.JRadioButton genreM;
    private javax.swing.JFileChooser jFileChooser6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label1;
    private javax.swing.JLabel label10;
    private javax.swing.JLabel label11;
    private javax.swing.JLabel label2;
    private javax.swing.JLabel label3;
    private javax.swing.JLabel label4;
    private javax.swing.JLabel label5;
    private javax.swing.JLabel label6;
    private javax.swing.JLabel label7;
    private javax.swing.JLabel label8;
    private javax.swing.JLabel label9;
    private javax.swing.JTextArea listaInteresses;
    private javax.swing.JTextField localidade;
    private javax.swing.JTextField mesNascimento;
    private javax.swing.JTextField nomeUtilizador;
    private javax.swing.JTextField pais;
    private javax.swing.JPasswordField password;
    private javax.swing.JRadioButton perfilPrivado;
    private javax.swing.JRadioButton perfilPublico;
    private javax.swing.JTextField primeiroUltimoNome;
    private javax.swing.JButton procuraFoto;
    private javax.swing.JPasswordField repetePassword;
    // End of variables declaration//GEN-END:variables

}
